import java.util.ArrayList;

/**
 * No copyright.
 * Class that synthesizes basic AdminTracker functions to allow for easy expansion.
 * @author Quan Nguyen
 * @version 1.00 2008-Aug-7
 */

public class AdminTracker
{
    private static String version = "beta 1.07";
    private static ArrayList<Event> events;
    private static ArrayList<Student> students;
    
    public static void main(String[] args)
    {
        if(args.length >= 1)
        {
            if(args[0].equals("-gui"))
                AdminTrackerGUI.main(new String[0]);
            else if(args[0].equals("-auto"))
                AutomatedAdminTracker.main(new String[0]);
        }
        else
        {
            System.out.println("AdminTracker usage: -gui for GUI, -auto for automated check.");
        }
    }
    
    public static void loadDatabases()
    {
        EventReader.readEvents();
        events = EventReader.getEvents();
        StudentReader.readStudents();
        students = StudentReader.getStudents();
    }

    public static String getAboutAdminTracker()
    {
        return "About AdminTracker\n------------------\nProgrammed for use by the Battalion Roster Officer of Unit Troy NJROTC."+
        " This applet was programmed by Quan Nguyen, Troy High School class of 2010. The AdminTracker consists of over 1200 lines"+
        " of code (*cough* reference soup *cough*) written in the Java programming language, as of version 1.06, or revision 15 in"+
        " the Subversion repository. The source code is available for free at this project's Google Code page at <http://code.google."+
        "com/p/admintracker>. This program would not have been possible were it not for the support of:\n\nChristian Reyes, Troy"+
        " High School class of 2009\nAaron Tran, Troy High School class of 2010\nCommander Stubblefield\nChief King\nFirst Sergeant"+
        " Lyon\n\nMr. Coglianese\nMr. Reid\nMr. Allen\nMr. Wittry\n\nAdminTracker 2008, version " + version + ".";
    }
    
    public static ArrayList<Event> getEvents()
    {
        return events;
    }
    
    public static ArrayList<Student> getStudents()
    {
        return students;
    }
    
    public static ArrayList<Event> getAllEvents()
    {
        ArrayList<Event> all = new ArrayList<Event>();
        for(int i = 0; i < events.size(); i++)
        {
            all.add(events.get(i));
            if(events.get(i).getClass().equals(FieldMeet.class))
            {
                for(int j = 0; j < ((FieldMeet)events.get(i)).getEvents().size(); j++)
                    all.add(((FieldMeet)events.get(i)).getEvents().get(j));
            }
        }
        return all;
    }
    
    public static String findStudentOnRosters(Student stu)
    {
        String results = "";
        for(int i = 0; i < events.size(); i++)
        {
            if(events.get(i).getTable().containsKey(new Integer(stu.getID())))
            {
                results = results + (events.get(i).toString() + ", " + events.get(i).getTable().get(new Integer(stu.getID())).toString() + " " + events.get(i).getDataName() + ".\n");
            }
            if(events.get(i).getClass().equals(FieldMeet.class))
            {
                FieldMeet fm = ((FieldMeet)events.get(i));
                for(int j = 0; j < fm.getEvents().size(); j++)
                {
                    if(fm.getEvents().get(j).getTable().containsKey(new Integer(stu.getID())))
                    {
                        results = results + (fm.getEvents().get(j).toString() + ", "
                        + fm.getEvents().get(j).getTable().get(new Integer(stu.getID())).toString() + " " + fm.getEvents().get(j).getDataName()+".\n");
                    }
                }
            }
        }
        if(results.equals(""))
            results = "No records.";
        return results;
    }
    
    public static int getAllForStudent(Student student, Category category)
    {
        int totalCount = 0;
        if(category.toString().equals("PARTICIPATION")) //handles differently
        {
            for(int i = 0; i < events.size(); i++)
            {
                if(student.attended(events.get(i)))
                    totalCount++;
            }
        }
        else
        {
            for(int i = 0; i < events.size(); i++)
            {
                boolean participated = student.attended(events.get(i));
                if(events.get(i).getClass().equals(FieldMeet.class))
                {
                    FieldMeet fm = ((FieldMeet)events.get(i));
                    for(int j = 0; j < fm.getEvents().size(); j++)
                    {
                        if(participated && fm.getEvents().get(j).getCategory().equals(category) && fm.getPerformed()[j] && fm.getEvents().get(j).getTable().containsKey(new Integer(student.getID())))
                            totalCount++;
                    }
                }
                else
                {
                    if(participated && events.get(i).getCategory().equals(category))
                        totalCount += addHoursOrAttendance(events.get(i), student);
                }
            } 
        }
        return totalCount;
    }
    
    private static int addHoursOrAttendance(Event event, Student student)
    {
        if(event.getCategory().toString().endsWith("SERVICE") && event.getTable().containsKey(new Integer(student.getID())))
        {
            int value = ((Integer)event.getTable().get(new Integer(student.getID()))).intValue();
            if(value > event.getCountable())
            {
                value = event.getCountable();
            }
            return value;
        }
        else if(event.getTable().containsKey(new Integer(student.getID())))
        {
            return 1;
        }
        return 0;
    }
}